#!/bin/bash

# For example: following config would generate link
# S51myservice in rc2.d, rc3.d, rc4.d, rc5.d and
# K49myservice in rc0.d, rc1.d, rc6.d

# Comments to support chkconfig on RedHat Linux
# run level, start order, stop order

# chkconfig: 2345 51 49
# description: Customized service written by Alvis.

start() {
    dd=`ps -ef | grep mlsql-engine | grep -i streamingpro-mlsql | awk '{print $2}'`
    if [ -z "${dd}" ];then
        echo 'This is my service, start mlsql'
        export SPARK_HOME=/opt/spark-3.0.0-preview2-bin-hadoop3.2
        export JAVA_HOME=/opt/jdk1.8.0_144
        echo '环境配置'
        nohup /opt/mlsql/mlsql-engine/start-default.sh 2>&1 >> /var/log/mlsql-engine.log &
        echo 'start mlsql-engine'
    else
        echo "已经启动"
    fi
}
stop() {
    echo 'This is my service, stop command'
    kill -9 `ps -ef | grep mlsql-engine | grep -i streamingpro-mlsql | awk '{print $2}'`
    echo 'stop mlsql-engine'
}
restart() {
    echo 'This is my service, restart command'
    echo ''
}
status() {
    echo 'This is my service, status command'
    echo `ps -ef | grep mlsql-engine | grep -i streamingpro-mlsql | awk '{print $2}'` > /var/run/mlsql/mlsql.pid
    dd=`ps -ef | grep mlsql-engine | grep -i streamingpro-mlsql | awk '{print $2}'`

    if [ -z $dd ];then
      echo 'mlsql-engine no start'
    else
      echo "mlsql-engine 已经启动 ${dd}"

    fi
    echo ''
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    *)
        echo 'Usage: service myservice {start|status|stop|restart}'
        echo ''
        exit 1
esac
exit 0
